package com.zr0726.news.service.impl;

import com.zr0726.news.dao.NewsRepository;
import com.zr0726.news.po.News;
import com.zr0726.news.service.NewsService;
import com.zr0726.news.util.MarkdownUtils;
import com.zr0726.news.vo.NewQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    NewsRepository newsRepository;

    //新闻管理中的列表（包含了查询）
    @Override
    public Page<News> listNews(Pageable pageable, NewQuery newQuery) {
        return newsRepository.findAll(new Specification<News>() {
            @Override
            public Predicate toPredicate(Root<News> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if(!"".equals(newQuery.getTitle()) && newQuery.getTitle()!=null){
                    predicates.add(cb.like(root.get("title"),"%"+newQuery.getTitle()+"%"));
                }
                if(newQuery.getTypeId() != null){
                    predicates.add(cb.equal(root.get("type").get("id"), newQuery.getTypeId()));
                }
                if(newQuery.isRecommend()){
                    predicates.add(cb.equal(root.get("recommend"), newQuery.isRecommend()));
                }
                cq.where(predicates.toArray(new Predicate[predicates.size()]));
                return null;
            }
        }, pageable);
    }

    @Override
    public News saveNews(News news) {
        if(news.getId() == null){
            news.setCreateTime(new Date());
            news.setUpdateTime(new Date());
        }
        return newsRepository.save(news);
    }

    @Override
    public News getNews(Long id) {
        return newsRepository.findById(id).orElse(null);
    }

    @Override
    public News updateNews(Long id, News news) {
        News news1 = newsRepository.findById(id).orElse(null);
        if(news1 == null){
            System.out.println("未获取更新对象");
        }
        BeanUtils.copyProperties(news, news1);
        news1.setUpdateTime(new Date());
        return newsRepository.save(news1);
    }

    @Override
    public void deleteNews(Long id) {
        newsRepository.deleteById(id);
    }

    @Override
    public Page<News> listNews(Pageable pageable) {
        return newsRepository.findAll(pageable);
    }

    @Override
    public List<News> listRecommendNewsTop(Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");
        Pageable pageable = PageRequest.of(0, size, sort);
        return newsRepository.findTop(pageable);
    }

    @Override
    public Page<News> listNews(String query, Pageable pageable) {
        return newsRepository.findByQuery(query, pageable);
    }

    @Override
    public News getAndConvertNews(Long id) {
        News news = newsRepository.findById(id).orElse(null);
        if(news == null){
            System.out.println("该新闻不存在");
            return null;
        }
        News news1 = new News();
        BeanUtils.copyProperties(news, news1);
        String content = news1.getContent();
        news1.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        return news1;
    }
}
